package com.sub.spark.streaming.instance;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        try {
            // 创建客户端的Socket，连接服务器
            Socket socket = new Socket("localhost", 1234);
            System.out.println("已连接到服务器");

            // 获取输入流和输出流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            // 读取用户输入并发送给服务器
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                System.out.println("服务器回复：" + in.readLine());
            }

            // 关闭连接
            socket.close();
        } catch (UnknownHostException e) {
            System.err.println("服务器不可达：" + e.getMessage());
        } catch (IOException e) {
            System.err.println("I/O错误：" + e.getMessage());
        }
    }
}
